/*
 * Created by Jaren at 2021/11/29 14:07
 */
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/common/date.dart';

class ProductProperty {
  static const String unknownSn = "0000000000000000";

  static const designedMaxSpeedSafetyModeKey = 'safety';
  static const designedMaxSpeedNormalModeKey = 'normal';
  static const designedMaxSpeedGeekModeKey = 'geek';

  /// 整机SN
  String sn = unknownSn;

  /// 产品型号
  ProductModel model = ProductModel.unknown;

  /// 设计满电续航里程，单位0.01km
  int designedRange = 0; // 单位0.01km

  /// 设计最大速度
  /// 某些车型可能存在不同模式下有不同最大限速值的情况
  /// 默认key[designedMaxSpeedNormalModeKey]都会存在
  /// 某些车型如V11 key[designedMaxSpeedGeekModeKey]会存在
  Map<String, int> designedMaxSpeed = {
    designedMaxSpeedNormalModeKey: 0
  }; // 单位0.01km/h

  /// 激活日期
  DateTime? activatedDate;

  /// 所有设备软硬件版本号
  VersionWrapper versionWrapper = VersionWrapper();

  ProductProperty(
      this.sn, this.model, this.activatedDate, this.versionWrapper) {
    setDefaultProperty(this);
  }

  static final instance =
      ProductProperty._byDefault(ProductModel.unknown, VersionWrapper());

  ProductProperty._byDefault(this.model, this.versionWrapper) {
    setDefaultProperty(this);
  }

  @override
  String toString() {
    return 'ProductProperty{sn: $sn, model: $model, designedRange: $designedRange, designedMaxSpeed: $designedMaxSpeed, activatedDate: $activatedDate, is activated?${isActivated()} versionWrapper: $versionWrapper}';
  }

  bool isActivated() {
    if (activatedDate == null) return true;
    return Datetime.isActivated(activatedDate!);
  }

  String formatActivatedDate() {
    return activatedDate == null
        ? "-"
        : "${activatedDate!.year.toString()}-${activatedDate!.month.toString().padLeft(2, '0')}-${activatedDate!.day.toString().padLeft(2, '0')}";
  }

  int getDefaultDesignedMaxSpeed() {
    return designedMaxSpeed[designedMaxSpeedNormalModeKey] ?? 0;
  }

  static setDefaultProperty(ProductProperty property) {
    property.designedRange = 0;
    property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 0;
    if (property.model.isV5Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      property.designedRange = 4000;

      if (property.model.isV5() || property.model.isV5SE()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
        property.designedRange = 2000;
      } else if (property.model.isV5Plus() ||
          property.model.isV5F() ||
          property.model.isV5D() ||
          property.model.isV5C()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      } else if (property.model.isV5E()) {
        property.designedRange = 4700;
      }
    } else if (property.model.isV8Series()) {
      if (property.model.isV8()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
        property.designedRange = 5000;
      } else if (property.model.isV8F()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3500;
        property.designedRange = 5500;
      } else if (property.model.isV8S()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3500;
        property.designedRange = 7000;
      }
    } else if (property.model.isV10Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 4000;
      property.designedRange = 7000;

      if (property.model.isV10F()) {
        property.designedRange = 10000;
      }
    } else if (property.model.isV11Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 5000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 5500;
      property.designedRange = 120000;

      if (property.model.isV11Y()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 6000;
        property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 6000;
      }
    } else if (property.model.isV12Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 6000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 7000;
      property.designedRange = 18000;

      if (property.model.isV12HT() || property.model.isV12Pro()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 5500;
        property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 6000;
      }
    } else if (property.model.isV13Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 6000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 9000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 9000;
      property.designedRange = 18000;

      if (property.model.isV13Pro()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 10000;
        property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 10000;
      }
    } else if (property.model.isV14Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 2500;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 7000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 8000;
      property.designedRange = 18000;
    } else if (property.model.isE20Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 2000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
      property.designedRange = 18000;
    } else if (property.model.isE10Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 2000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
      property.designedRange = 18000;
    } else if (property.model.isE15Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 2000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
      property.designedRange = 18000;
    } else if (property.model.isP6Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 15000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 15000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 15000;
      property.designedRange = 18000;
    } else if (property.model.isV6Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 2500;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      property.designedRange = 18000;
    } else if (property.model.isV9Series()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 4500;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 4500;
      property.designedRange = 18000;
    } else if (property.model.isV12sSeries()) {
      property.designedMaxSpeed[designedMaxSpeedSafetyModeKey] = 6000;
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 6000;
      property.designedMaxSpeed[designedMaxSpeedGeekModeKey] = 7000;
      property.designedRange = 18000;
    } else if (property.model.isL8Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      property.designedRange = 2000;
      if (property.model.isL8FLike()) {
        property.designedRange = 3500;
      }
      if (property.model.isL8Fch()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
      }
      if (property.model.isL8Fno() || property.model.isL8DF()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      }
      if (property.model.isL8D() || property.model.isL8S()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      }
      if (property.model.isL8SPlus()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2200;
      }
      if (property.model.isL8No()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2400;
      }
    } else if (property.model.isLS1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      property.designedRange = 9500;

      if (property.model.isLS1German()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
        property.designedRange = 9500;
      } else if (property.model.isLS1HS()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 4000;
        property.designedRange = 9500;
      }
    } else if (property.model.isLA1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      property.designedRange = 3000;
      if (property.model.isLA1FGerman()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2000;
      } else if (property.model.isLA1()) {
        property.designedRange = 2000;
      }
    } else if (property.model.isLC1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
      property.designedRange = 3500;

      if (property.model.isLC1Pro()) {
        property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3500;
        property.designedRange = 4800;

        if (property.model.isLC1ProEU()) {
          property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 2500;
        }
      }
    } else if (property.model.isLE1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      property.designedRange = 3000;
    } else if (property.model.isLR1Series() ||
        property.model.isRSVeloSeries()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 9000;
      property.designedRange = 9000;
    } else if (property.model.isP1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      property.designedRange = 0;
    } else if (property.model.isP2Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 3000;
      property.designedRange = 0;
    } else if (property.model.isR1Series()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 1500;
      property.designedRange = 0;
    } else if (property.model.isElephantModel()) {
      property.designedMaxSpeed[designedMaxSpeedNormalModeKey] = 1000;
      property.designedRange = 0;
    }
  }
}
